define(function(require, exports){
	var $       = require("jquery");
	var util    = require('util');
	var pubjs   = require('pubjs');
	var view    = require('@base/view');
	var utilDSP = require('modules/common');
	var tip = require('@base/tip');
	var budgetTip = require('plan/modules/budgetTip');

	var Main = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'class': 'P-advertiserListInfo',
				'url_info': 'campaign/info',
				'grid_config': null, // 列表配置
				'gridName': 'customer',
				'vModel':{
					'Name': '',
					'Id': '',
					'timeRange': '',
					'showHead': false
				},

			});
			this.Super('init', arguments);
		},
		afterBuild:function(){
			var self = this;
			var c = self.getConfig();
			var el = self.getDOM();
			var doms = self.$doms = {};
			self.append($([
				'<div class="M-formHead P-advertiserListInfoHead" v-show="showHead">',
					'<div class="name"></div>',
					'<ul class="detail">',
						'<li class="ml30">',
							'<div>' + LANG('ID') + '</div>',
							'<div class="content">{{ Id }}</div>',
						'</li>',
						'<li>',
							'<div>' + LANG('投放时间') + '</div>',
							'<div class="content">{{ timeRange }}</div>',
						'</li>',
						'<li class="ml30">',
							'<div>' + LANG('推广组预算') + '</div>',
							'<span class="link content dib">' + LANG('查看预算') + '</span>',
						'</li>',
					'</ul>',
				'</div>',
			].join('')));
			doms.box = el.find('.link');
			// 构建弹层
			if (!self.get('tooltip')){
				doms.tooltipCon = self.create('tooltip', tip.tooltip, {
					'anchor': doms.box
					,'autoShow': true
					,'pos': 'bm' // 定位模式
					,'width': 360
					,'hideDuration': 0
					,'arrowAlign': 'middle'
					,'autoHide': 'click_body click_anchor click leave'
				}).getContainer();
			}
			var tipTableCon = $('<div class="M-userToolbarCon"/>').appendTo(doms.tooltipCon);
			doms.tipTable = $('<div class="M-userToolbarConUserInfo pd10"/>').appendTo(tipTableCon);
			self.create('tipTable', budgetTip.info, {
					'target': doms.tipTable
			});
			// 绑定事件
			self.uiBind(doms.box, 'hover', 'eventHover');
			this.vm.mount();
			self.buildFromTemplate({
			});
		},
		load: function(id){
			var self = this;
			var c = self.getConfig();
			pubjs.mc.send(c.url_info, {'_id': id}, self.onData.bind(self));
			return self;
		},
		eventHover: function(){
			var self = this;
			var doms = self.$doms;
			self.$.tooltip.show();
			doms.box.toggleClass('act', true);
			return false;
		},
		onData: function(err, data) {
			if (err) {
				pubjs.error(err.message);
				pubjs.alert(err.message);
				return false;
			}
			this.vm.$.Name = data.Name;

			this.vm.$.Id = data._id;
			this.vm.$.timeRange = "-";
			var self = this;
			if(data.StartTime && data.EndTime){
				self.vm.$.timeRange = util.date("Y年m月d日", data.StartTime) + "至" + util.date("Y年m月d日", data.EndTime);
			}
			this.renderName(
				data
			);
			this.$.tipTable.setData({
				TotalLimit:data.MainStrategy.TotalLimit,
				DailyLimit:data.MainStrategy.DailyLimit
			})
			this.vm.$.showHead = true;
		},
		reset: function(){
			this.vm.reset();
			return this;
		},
		renderName: function(data){
			if (data._id == 0)
			{
				return data.Name;
			}
			/**
				Status：
				1 正常/进行中
				2 暂停
				3 草稿
				4 未开始
				5 已结束
				6 超预算
			**/
			var icon = [
				['close', LANG('关闭')],
				['running', LANG('进行中')],
				['pause', LANG('已暂停')],
				['draft', LANG('草稿')],
				['notStart ', LANG('未开始')],
				['completed', LANG('已结束')],
				['overBudget', LANG('超预算')],
				['deleted', LANG('已归档')]
			];

			var num = 0;
			var start = data['StartTime'];
			var end = data['EndTime'];
			var now = util.date('Ymd', new Date());
			if (start > now) {
				num =  4;		//未开始
			}
			if (start <= now && (+end === 0 || end >= now) )
			{
				if (data['Exceed']) {
					num =  6;		//超预算
				} else {
					num =  1;		//进行中
				}
			}
			if (+end !== 0 &&  end < now) {
				num =  5;			//已结束
			}

			switch (true)
			{
				case data['Status'] == 1:
					num = 1;
					break;
				case data['Status'] == 2:
					num = 2;
					break;
				case data['Status'] == 3:
					num = 3;
					break;
				case !!data['IsDeleted']:
					num = 7;
					break;
			}
			var status = icon[num];
			var iEdit = $('<a class="dib" target="_self"  href="'+'#serving/campaignEdit/' + data._id+'"><i class="uk-icon-pencil mt10 ml10"/></a>');
			var iStatus = $("<i title="+ status[1] +" class= '"+"cur_p mr5 status status-"+status[0]+"'></i>");
			var el = this.getDOM().find('.M-formHead .name').addClass('P-campaignList');
			el.empty();
			el.prepend(iStatus).append(data.Name).append(iEdit);
			return status;
		}
	});
	exports.main = Main;
});